package edu.chinasoft.servlet;

import edu.chinasoft.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name="myUserLogoutServlet",value="/servlet/userlogoutservlet")
public class  UserLogoutServlet  extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(UserLogoutServlet.class);
	
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.info(" UserLogoutServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession(false);
		User user = null;

		if(session!=null) {
			user= (User)session.getAttribute("userLogin");
		}
		
		if(user==null) {      //表示用户没有登录
			return;
		}else {                //表示用户登录
			session.removeAttribute("userLogin");
			response.sendRedirect(""+request.getContextPath()+"/main.jsp");
		}




		logger.info(" UserLogoutServlet >doGet end.......");
	}
	
	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.info(" UserLogoutServlet >doPost start.......");
		this.doGet(request, response);

		logger.info(" UserLogoutServlet >doPost end.......");
	}
	
}